package com.jltx.xq.relaxnewspro.activity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.view.View;

import com.google.gson.Gson;
import com.jltx.xq.relaxnewspro.R;
import com.jltx.xq.relaxnewspro.database.cache.DailyCache;
import com.jltx.xq.relaxnewspro.database.table.DailyTable;
import com.jltx.xq.relaxnewspro.utils.CONSTANT;
import com.jltx.xq.relaxnewspro.utils.DailyDetailsBean;
import com.jltx.xq.relaxnewspro.utils.DisplayUtil;
import com.jltx.xq.relaxnewspro.utils.HttpUtil;
import com.jltx.xq.relaxnewspro.utils.Settings;
import com.jltx.xq.relaxnewspro.utils.StoryBean;
import com.jltx.xq.relaxnewspro.utils.XqLog;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

/**
 * Created by bo on 2017/5/5.
 */

public class DailyDetailsActivity extends BaseDetailsActivity implements SensorEventListener {

    private boolean isShakeMode = false;
    private DailyDetailsBean dailyDetailsBean;
    private String url;
    private int id;
    private String imageUrl;
    private String title;
    public String body;
    private DailyCache cache;
    private StoryBean storyBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getData();
        initView();


    }

    protected void onDataRefresh(){
        Request.Builder builder = new Request.Builder();
        builder.url(url);
        final Request request = builder.build();
        HttpUtil.enqueue(request, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                handler.sendEmptyMessage(CONSTANT.ID_FAILURE);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String res = response.body().string();
                XqLog.d(res);
                Gson gson = new Gson();
                dailyDetailsBean = gson.fromJson(res,DailyDetailsBean.class);
                cache.ececSQL(DailyTable.updateBodyContent(DailyTable.NAME,dailyDetailsBean.getTitle(),dailyDetailsBean.getBody()));
                cache.ececSQL(DailyTable.updateBodyContent(DailyTable.COLLECTION_NAME,dailyDetailsBean.getTitle(),dailyDetailsBean.getBody()));
                cache.ececSQL(DailyTable.updateBodyContent(DailyTable.NAME,dailyDetailsBean.getTitle(),dailyDetailsBean.getImage()));
                cache.ececSQL(DailyTable.updateBodyContent(DailyTable.COLLECTION_NAME,dailyDetailsBean.getTitle(),dailyDetailsBean.getImage()));

                imageUrl = dailyDetailsBean.getImage();
                body = dailyDetailsBean.getBody();
                handler.sendEmptyMessage(CONSTANT.ID_SUCCESS);
            }
        });
    }

    private void getData(){
        storyBean = new StoryBean();
        url = getIntent().getStringExtra(getString(R.string.id_url));
        id = getIntent().getIntExtra(getString(R.string.id_id),0);
        body = getIntent().getStringExtra(getString(R.string.id_body));
        title = getIntent().getStringExtra(getString(R.string.id_title));
        imageUrl = getIntent().getStringExtra(getString(R.string.id_imageurl));
        isCollected = getIntent().getBooleanExtra(getString(R.string.id_collection),false);

        storyBean.setId(id);
        storyBean.setBody(body);
        storyBean.setTitle(title);
        storyBean.setLargepic(imageUrl);
        storyBean.setImages(new String[]{getIntent().getStringExtra(getString(R.string.id_small_image))});
    }

    protected void initView(){
        super.initView();


        cache = new DailyCache(handler);
        if(body == "" || body == null||imageUrl == null || imageUrl == "") {
            onDataRefresh();
        }else{
            handler.sendEmptyMessage(CONSTANT.ID_FROM_CACHE);
        }
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case CONSTANT.ID_FAILURE:
                    break;
                case CONSTANT.ID_SUCCESS:

                case CONSTANT.ID_FROM_CACHE:
                    if(HttpUtil.isWIFI == true || Settings.getsInstance().getBoolean(Settings.NO_PIC_MODE,false) == false) {
                        setMainContentBg(imageUrl);
                    }
                    scrollView.setVisibility(View.VISIBLE);
                    scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                        @Override
                        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                            topImage.setTranslationY(Math.max(-scrollY / 2, -DisplayUtil.dip2px(getBaseContext(),170)));
                        }
                    });
                    contentView.loadDataWithBaseURL("file:///android_asset/", "<link rel=\"stylesheet\" type=\"text/css\" href=\"dailycss.css\"/>"+ body,"text/html","utf-8",null);

                    hideContentLoading();
                    break;
            }
            return false;
        }
    }){

    };

    protected void removeFromCollection(){
        cache.ececSQL(DailyTable.updateCollectionFlag(storyBean.getTitle(),0));
        cache.ececSQL(DailyTable.deleteCollectionFlag(storyBean.getTitle()));
    }

    protected void addToCollection(){
        cache.ececSQL(DailyTable.updateCollectionFlag(storyBean.getTitle(),1));
        cache.addToCollection(storyBean);
    }

    @Override
    protected String getShareInfo() {
        return null;
    }


    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
